﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using System.Text;
using CamHelper.Engine;
using CamHelper.Interpreter;
using Ninject;
using Ninject.MockingKernel.RhinoMock;
using NUnit.Framework;
using Rhino.Mocks;
using SystemWrapper.IO;

namespace CamHelper.Tests.Interpreter
{
    [TestFixture]
    public class ParameterManagerTest
    {

        #region CreateInstance

        private ParameterManager CreateInstance(string contentZmiOpUser = "", string contentZmiOpCam = "", string contentZmiOpCamTurn = "")
        {
            var pathConfig = @"C:\Config";
            var pathHome = @"C:\Home";
            var pathZmiOpUser = Path.Combine(pathConfig, "ZmiOpUser.par");
            var pathZmiOpCam = Path.Combine(pathHome, "bin\\ZmiOpCam.par");

            var configMock = MockRepository.GenerateMock<IConfig>();
            configMock.Expect(x => x.TopConfigDirectory).Return(pathConfig);
            configMock.Expect(x => x.TopHomeDirectory).Return(pathHome);

            var fileMock = MockRepository.GenerateMock<IFileWrap>();
            fileMock.Expect(x => x.Exists(Arg<String>.Is.Equal(pathZmiOpUser))).Return(contentZmiOpUser.Length > 0);
            fileMock.Expect(x => x.ReadAllText(pathZmiOpUser, Encoding.Default)).Return(contentZmiOpUser);
            fileMock.Expect(x => x.Exists(Arg<String>.Is.Equal(pathZmiOpCam))).Return(contentZmiOpCam.Length > 0);
            fileMock.Expect(x => x.ReadAllText(pathZmiOpCam, Encoding.Default)).Return(contentZmiOpCam);

            var kernel = new RhinoMocksMockingKernel();
            kernel.Bind<IConfig>().ToConstant(configMock);
            kernel.Bind<IFileWrap>().ToConstant(fileMock);
            return kernel.Get<ParameterManager>();
        }

        #endregion

        #region ZmiOpCamAndZmiOpUser1

        [Test]
        public void ZmiOpCamAndZmiOpUser1()
        {
            var contentZmiOpUser =
@"[Par]

# app_ret_ang = 20";
            var contentZmiOpCam =
@"[Par]

# app_ret_ang = 60";

            var pm = CreateInstance(contentZmiOpUser: contentZmiOpUser, contentZmiOpCam: contentZmiOpCam);
            Assert.That(pm.AllParameter.Count, Is.EqualTo(1));
        }

        #endregion

        #region ZmiOpCamAndZmiOpUser2

        [Test]
        public void ZmiOpCamAndZmiOpUser2()
        {
            var contentZmiOpUser =
@"[Par]

# app_ret_ang = 20";
            var contentZmiOpCam =
@"[Par]

# dep_ret_ang = 90
# app_ret_ang = 60";

            var pm = CreateInstance(contentZmiOpUser: contentZmiOpUser, contentZmiOpCam: contentZmiOpCam);
            Assert.That(pm.AllParameter.Count, Is.EqualTo(2));
        }

        #endregion

        #region SearchEmpty

        [Test]
        public void SearchEmpty()
        {
            var contentZmiOpUser =
@"[Par]

# app_ret_ang = 20";
            var contentZmiOpCam =
@"[Par]

# dep_ret_ang = 90
# app_ret_ang = 60

[Global]
# mill = 23";

            var pm = CreateInstance(contentZmiOpUser: contentZmiOpUser, contentZmiOpCam: contentZmiOpCam);

            var ps = pm.Search(categorie: String.Empty, word: String.Empty, onlyZmiOpUser: true);
            Assert.That(ps.Count, Is.EqualTo(1));

            ps = pm.Search(categorie: String.Empty, word: String.Empty, onlyZmiOpUser: false);
            Assert.That(ps.Count, Is.EqualTo(3));
        }

        #endregion

        #region SearchCategorie

        [Test]
        public void SearchCategorie()
        {
            var contentZmiOpUser =
@"[Par]

# app_ret_ang = 20";
            var contentZmiOpCam =
@"[Par]

# dep_ret_ang = 90
# app_ret_ang = 60

[Global]
# mill = 23";

            var pm = CreateInstance(contentZmiOpUser: contentZmiOpUser, contentZmiOpCam: contentZmiOpCam);

            var ps = pm.Search(categorie: "Par", word: String.Empty, onlyZmiOpUser: true);
            Assert.That(ps.Count, Is.EqualTo(1));

            ps = pm.Search(categorie: "Par", word: String.Empty, onlyZmiOpUser: false);
            Assert.That(ps.Count, Is.EqualTo(2));
        }

        #endregion

        #region SearchWord

        [Test]
        public void SearchWord()
        {
            var contentZmiOpUser =
@"[Par]

# dep_ret_ang = 20
# app_ret_ang = 30

[Global]
# mill = 23";
            var contentZmiOpCam =
@"[Par]

# dep_ret_ang = 40
# app_ret_ang = 60

[Global]
# mill = 46
# millf = 47";

            var pm = CreateInstance(contentZmiOpUser: contentZmiOpUser, contentZmiOpCam: contentZmiOpCam);

            var ps = pm.Search(categorie: String.Empty, word: "mill", onlyZmiOpUser: true);
            Assert.That(ps.Count, Is.EqualTo(1));

            ps = pm.Search(categorie: String.Empty, word: "mill", onlyZmiOpUser: false);
            Assert.That(ps.Count, Is.EqualTo(2));
        }

        #endregion

        #region SearchWordAndCategorie

        [Test]
        public void SearchWordAndCategorie()
        {
            var contentZmiOpUser =
@"[Par]

# dep_ret_ang = 20
# app_ret_ang = 30

[Global]
# dep_mill = 23
# app_mill = 9";
            var contentZmiOpCam =
@"[Par]

# dep_ret_ang = 40
# app_ret_ang = 60

[Global]
# dep_mill = 46
# dep_millf = 47
# app_mill = 99";

            var pm = CreateInstance(contentZmiOpUser: contentZmiOpUser, contentZmiOpCam: contentZmiOpCam);

            var ps = pm.Search(categorie: String.Empty, word: "dep", onlyZmiOpUser: true);
            Assert.That(ps.Count, Is.EqualTo(2));

            ps = pm.Search(categorie: String.Empty, word: "dep", onlyZmiOpUser: false);
            Assert.That(ps.Count, Is.EqualTo(3));

            ps = pm.Search(categorie: "Global", word: "dep", onlyZmiOpUser: true);
            Assert.That(ps.Count, Is.EqualTo(1));

            ps = pm.Search(categorie: "Global", word: "dep", onlyZmiOpUser: false);
            Assert.That(ps.Count, Is.EqualTo(2));
        }

        #endregion

    }
}
